import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { AIModel, AIResponse, UserInfo } from '@/api'

export const useAIStore = defineStore('ai', () => {
  // 状态
  const userInfo = ref<UserInfo | null>(null)
  const aiModels = ref<AIModel[]>([])
  const selectedModels = ref<string[]>([])
  const chatHistory = ref<AIResponse[]>([])
  const isLoading = ref(false)

  // 计算属性
  const selectedModelsList = computed(() => 
    aiModels.value.filter(model => selectedModels.value.includes(model.id))
  )

  const freeModels = computed(() => 
    aiModels.value.filter(model => model.free)
  )

  const paidModels = computed(() => 
    aiModels.value.filter(model => !model.free)
  )

  // 方法
  const setUserInfo = (info: UserInfo) => {
    userInfo.value = info
  }

  const setAIModels = (models: AIModel[]) => {
    aiModels.value = models
  }

  const selectModel = (modelId: string) => {
    if (selectedModels.value.includes(modelId)) {
      selectedModels.value = selectedModels.value.filter(id => id !== modelId)
    } else {
      selectedModels.value.push(modelId)
    }
  }

  const selectAllModels = () => {
    selectedModels.value = aiModels.value.map(model => model.id)
  }

  const clearSelectedModels = () => {
    selectedModels.value = []
  }

  const addChatResponse = (response: AIResponse) => {
    chatHistory.value.unshift(response)
  }

  const setLoading = (loading: boolean) => {
    isLoading.value = loading
  }

  return {
    // 状态
    userInfo,
    aiModels,
    selectedModels,
    chatHistory,
    isLoading,
    
    // 计算属性
    selectedModelsList,
    freeModels,
    paidModels,
    
    // 方法
    setUserInfo,
    setAIModels,
    selectModel,
    selectAllModels,
    clearSelectedModels,
    addChatResponse,
    setLoading,
  }
}) 